<template>
  <div class="wrap-wrap">
    <common-head title="登录页"></common-head>
    <div class="wrap">
      <div class="logo">
        <img src="@assets/images/logo.png" alt="">
      </div>
      <div class="form">
        <van-form @submit="onSubmit">
          <van-field
            v-model="user.username"
            name="username"
            label="用户名"
            placeholder="用户名"
            :rules="[
              { required: true, message: '请填写用户名' },
              { pattern: /^[\u4e00-\u9fa5]{0,}$/, message: '用户名只能输入中文'}
            ]"
          />
          <van-field
            v-model="user.pwd"
            type="password"
            name="pwd"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
          <div style="margin: 16px;">
            <van-button round block type="info" native-type="submit">点击登录</van-button>
          </div>
        </van-form>
        <p class="reg-btn" @click="$router.push('/registry')">点击前往注册</p>
        <div class="login-others-wrap">
          <div class="login-others">
            <p>第三方登录</p>
            <div class="line"></div>
          </div>
          <div class="icon-others">
            <div class="icon-left">
              <van-icon class-prefix="icon" name="tubiao215">扣扣</van-icon>
            </div>
            <div class="icon-right">
              <van-icon class-prefix="icon" name="weixin">微信</van-icon>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import CommonHead from '@components/CommonHead'
import { userLogin } from '@api'
import { Toast } from 'vant'
export default {
  components: {
    CommonHead
  },
  data () {
    return {
      user: {
        username: '',
        pwd: ''
      }
    }
  },
  methods: {
    onSubmit (params) {
      userLogin({
        ...params,
        deviceId: '666',
        deviceName: 'phone'
      }).then(res => {
        console.log(res)
        if (res.data.code === 0) {
          localStorage.setItem('token', res.data.data.token)
          Toast({
            type: 'suceess',
            message: '登陆成功',
            duration: 2000,
            onClose: () => {
              if (this.$route.query.from) {
                this.$router.replace(this.$route.query.from)
              } else {
                this.$router.replace('/home')
              }
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.wrap{
  .logo{
    background-color: #fff;
    text-align: center;
    line-height: 120px;
    padding-top: 50px;
    img{
      width: 138px;
      height: 57px;
    }
  }
  .form{
    background-color: #fff;
    .reg-btn{
      line-height: 100px;
      text-align: center;
      font-size: 14px;
    }
    .login-others-wrap{
      text-align: center;
      .login-others{
        position: relative;
        height: 50px;
        >p{
          position: absolute;
          top: -4px;
          left: 123px;
          background-color: #fff;
          padding: 0 30px;
        }
        .line{
          height: 1px;
          width: 280px;
          background-color: #ccc;
          margin: 0 auto;
        }
      }
      .icon-others{
        display: flex;
        justify-content: space-evenly;
      }
    }
  }
}
</style>
